package com.game.ctrler
{
	
	import base.BaseBullet;
	import base.BaseHero;
	import base.BaseMonster;
	
	import com.dusk.game.Logger;
	import com.dusk.util.McUtil;
	import com.dusk.util.ResUtil;
	import com.game.engine.MainGame;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import com.game.manager.GameSceneManager;
	import com.game.manager.SoundManager;
	import com.game.view.layer.GameCartoon;
	import com.game.view.layer.GameFail;
	import com.game.view.layer.GameWin;
	import com.greensock.TweenMax;
	
	import config.Config;
	
	import flash.utils.getTimer;
	
	public class GameLogicCtrler
	{
		public function GameLogicCtrler(game:MainGame)
		{
			mainGame = game;
			init();
		}
		
		public var mainGame:MainGame;
		
		private function get gc():Config
		{
			return Config.getIns();
		}
		
		public function init():void
		{
			EventManager.getIns().registerListen(GameEventType.PAUSE_GAME,pauseGame);
			EventManager.getIns().registerListen(GameEventType.CONTINUE_GAME,continueGame);
			EventManager.getIns().registerListen(GameEventType.ENTER_SUB_LEVEL,enterSubLevel);
			EventManager.getIns().registerListen(GameEventType.RESTART_GAME,restartGame);
			EventManager.getIns().registerListen(GameEventType.HERO_DEAD,heroDead);
			EventManager.getIns().registerListen(GameEventType.CREATE_MONSTER,createMonster);
			EventManager.getIns().registerListen(GameEventType.KILL_MONSTER,killMonster);
			EventManager.getIns().registerListen(GameEventType.MONSTER_DEAD,monsterDead);
			EventManager.getIns().registerListen(GameEventType.GAME_FAILED,levelFailed);
			EventManager.getIns().registerListen(GameEventType.GAME_VICTOR,levelVictor);
		}
		
		public function destroy():void
		{
			EventManager.getIns().removeListen(GameEventType.PAUSE_GAME,pauseGame);
			EventManager.getIns().removeListen(GameEventType.CONTINUE_GAME,continueGame);
			EventManager.getIns().removeListen(GameEventType.ENTER_SUB_LEVEL,enterSubLevel);
			EventManager.getIns().removeListen(GameEventType.RESTART_GAME,restartGame);
			EventManager.getIns().removeListen(GameEventType.HERO_DEAD,heroDead);
			EventManager.getIns().removeListen(GameEventType.CREATE_MONSTER,createMonster);
			EventManager.getIns().removeListen(GameEventType.KILL_MONSTER,killMonster);
			EventManager.getIns().removeListen(GameEventType.MONSTER_DEAD,monsterDead);
			EventManager.getIns().removeListen(GameEventType.GAME_FAILED,levelFailed);
			EventManager.getIns().removeListen(GameEventType.GAME_VICTOR,levelVictor);
			mainGame = null;
		}
		
		public function pauseGame(data:Object = null):void //{time:0}
		{
			if(data && data.time)
			{
				TweenMax.delayedCall(data.time,continueGame);
			}
			for each (var hero:BaseHero in gc.team.getHeroArray())
			{
				for each (var heroBullet:BaseBullet in hero.magicBulletArray)
				{
					McUtil.stopAllChildren(heroBullet);
				}
			}
			for each (var mon:BaseMonster in gc.pWorld.getMonsterArray())
			{
				McUtil.stopAllChildren(mon);
				if (!mon.isReadyToDestroy && mon.magicBulletArray)
				{
					for each(var monBullet:BaseBullet in mon.magicBulletArray)
					{
						McUtil.stopAllChildren(monBullet);
					}
				}
			}
			gc.keyboardControl.stopKeyboardControl();
			TweenMax.pauseAll(true, true);
			mainGame.battleData.pausePre = getTimer();
			mainGame.stopStep();
		}
		
		public function continueGame(data:Object = null):void
		{
			if(gc.team)
			{
				for each (var hero:BaseHero in gc.team.getHeroArray())
				{
					for each (var heroBullet:BaseBullet in hero.magicBulletArray)
					{
						McUtil.startAllChildren(heroBullet);
					}
				}
			}
			if(gc.pWorld)
			{
				for each (var mon:BaseMonster in gc.pWorld.getMonsterArray())
				{
					McUtil.startAllChildren(mon);
					if (!mon.isReadyToDestroy && mon.magicBulletArray)
					{
						for each(var monBullet:BaseBullet in mon.magicBulletArray)
						{
							McUtil.startAllChildren(monBullet);
						}
					}
				}
			}
			gc.keyboardControl.continueKeyboardControl();
			TweenMax.resumeAll();
			mainGame.battleData.pauseTotal += getTimer() - mainGame.battleData.pausePre;
			mainGame.startStep();
		}
		
		public function restartGame(data:Object):void
		{
			if (mainGame.levelData && mainGame.levelData.subLevelID != null)
			{
				GameSceneManager.getIns().switchScene(GameSceneManager.START_FIGHTING,mainGame.levelData.levelID);
				mainGame.destroyGame();
			}
		}
		
		public function enterSubLevel(data:Object = null):void //{level:"1_1"}
		{
			if(data)
			{
				GameSceneManager.getIns().switchScene(GameSceneManager.START_FIGHTING,data.level);
			}
			else if(mainGame.levelData.subLevelID != null)
			{
				GameSceneManager.getIns().switchScene(GameSceneManager.START_FIGHTING,mainGame.levelData.subLevelID);
			}
		}
		
		public function createMonster(data:Object):void //{type:0,x:0,y:0}
		{
			mainGame.createMonster(data.type,data.x,data.y);
		}
		
		public function monsterDead(name:String):void
		{
//			判断任务是否完成
//			gc.allTask.killMonster(name);
		}
		
		public function heroDead(data:Object):void //{x:0,y:0}
		{
			var afterDead:* = ResUtil.getNewObj("PlayerDeadMc");
			afterDead.x = data.x;
			afterDead.y = data.y;
			gc.gameScene.addChild(afterDead);
			TweenMax.delayedCall(2.5, function ():void
			{
				if (gc.team.getHeroArray().length == 0)
				{
					EventManager.getIns().dispatchEvent(GameEventType.GAME_FAILED);
				}
			});
		}
		
		public function killMonster():void
		{
			for each (var mon:BaseMonster in gc.pWorld.getMonsterArray())
			{
				mon.reduceHp(9999999);
			}
		}
		
		public function levelVictor():void
		{
			GameSceneManager.getIns().addScene(new GameWin());
			if(mainGame.levelData.levelOverCartoon)
			{
				var cartoon:GameCartoon = new GameCartoon();
				cartoon.show(mainGame.levelData.levelOverCartoon, GameCartoon.SHOW_VICTOR);
			}
			SoundManager.playSound("Victory");
			SoundManager.clearLoop();
			if (mainGame.levelData && mainGame.levelData.nextLevelID != null)
			{
				var arr:Array = mainGame.levelData.getNextLevelIDArray();
				if (gc.userData.maxStage < arr[0])
				{
					gc.userData.maxStage = arr[0];
					gc.userData.maxLevel = arr[1];
				}
				else if (gc.userData.maxStage == arr[0])
				{
					if (gc.userData.maxLevel < arr[1])
					{
						gc.userData.maxLevel = arr[1];
					}
				}
			}
			gc.memory.saveGame(gc.saveId, true);
			mainGame.destroyGame();
		}
		
		public function levelFailed():void
		{
			GameSceneManager.getIns().addScene(new GameFail());
			SoundManager.playBgm("failed");
			mainGame.destroyGame();
		}
	}
}
